package com.glsc.ngateway.filetransfer.controller;

import cn.hutool.core.io.IoUtil;
import com.glsc.ngateway.common.api.filetransfer.resp.FileTransferResponse;
import com.glsc.ngateway.common.api.filetransfer.dto.SmbFileDto;
import com.glsc.ngateway.filetransfer.service.FileTransferService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@Api(value = "文件中转服务", tags = {"文件中转服务"})
@RequestMapping("/")
public class FileTransferController {

    @Resource
    private FileTransferService fileTransferService;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @ApiOperation("查看文件目录列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "ip", value = "服务器ip", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "userAccount", value = "服务器账户", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "passwd", value = "服务器密码", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "shareFolderName", value = "文件目录", required = true, paramType = "path", dataTypeClass = String.class)})
    @GetMapping("/listFiles")
    public FileTransferResponse<List<SmbFileDto>> listFiles(String ip, String userAccount, String passwd, String shareFolderName) {
        logger.info("listFiles,ip:{},account:{},passwd:{},sharefoldername:{}", ip, userAccount, passwd, shareFolderName);
        List<SmbFileDto> res = fileTransferService.listFiles(ip, userAccount, passwd, shareFolderName);
        return FileTransferResponse.success(res);
    }

    @ApiOperation("检查文件/文件目录是否存在")
    @ApiImplicitParams({@ApiImplicitParam(name = "ip", value = "服务器ip", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "userAccount", value = "服务器账户", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "passwd", value = "服务器密码", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "shareFolderName", value = "文件目录", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "remoteFileName", value = "文件名", required = true, paramType = "path", dataTypeClass = String.class)})
    @GetMapping("/checkFileExist")
    public FileTransferResponse<Boolean> checkFileExist(String ip, String userAccount, String passwd, String shareFolderName, String remoteFileName) {
        logger.info("checkFileExist,ip:{},account:{},passwd:{},sharefoldername:{}", ip, userAccount, passwd, shareFolderName);
        boolean res = fileTransferService.checkFileExist(ip, userAccount, passwd, shareFolderName, remoteFileName);
        return FileTransferResponse.success(res);
    }

    @ApiOperation("推送文件到共享目录下")
    @ApiImplicitParams({@ApiImplicitParam(name = "ip", value = "服务器ip", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "userAccount", value = "服务器账户", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "passwd", value = "服务器密码", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "shareFolderName", value = "文件目录", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "file", value = "文件", required = true, paramType = "form", dataTypeClass = MultipartFile.class), @ApiImplicitParam(name = "cover", value = "是否覆盖（Y/N）", required = true, paramType = "path", dataTypeClass = String.class)})
    @PostMapping("/putFile")
    public FileTransferResponse<String> putFile(String ip, String userAccount, String passwd, String shareFolderName, String cover, @RequestPart("file") MultipartFile file) {
        logger.info("putFile,ip:{},account:{},passwd:{},sharefoldername:{},cover:{}", ip, userAccount, passwd, shareFolderName, cover);
        fileTransferService.putFile(ip, userAccount, passwd, shareFolderName, cover, file);
        return FileTransferResponse.success("res");
    }

    @ApiOperation("从共享目录下获取文件")
    @ApiImplicitParams({@ApiImplicitParam(name = "ip", value = "服务器ip", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "userAccount", value = "服务器账户", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "passwd", value = "服务器密码", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "shareFolderName", value = "文件目录", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "remoteFileName", value = "文件名", required = true, paramType = "path", dataTypeClass = String.class)})
    @GetMapping("/getFile")
    public FileTransferResponse<String> getFile(String ip, String userAccount, String passwd, String shareFolderName, String remoteFileName, HttpServletResponse response) {
        logger.info("getFile,ip:{},account:{},passwd:{},sharefoldername:{}", ip, userAccount, passwd, shareFolderName);
        OutputStream out = null;
        try {
            out = response.getOutputStream();
            response.setHeader("Content-Disposition", "attachment;" + "filename=" + URLEncoder.encode(remoteFileName, StandardCharsets.UTF_8));
            fileTransferService.getFile(ip, userAccount, passwd, shareFolderName, remoteFileName, out);
        } catch (Exception e) {
            FileTransferResponse.failError("获取文件失败!", e.getLocalizedMessage());
        } finally {
            IoUtil.close(out);
        }
        return FileTransferResponse.success("res");
    }

    @ApiOperation("删除共享目录下的文件")
    @ApiImplicitParams({@ApiImplicitParam(name = "ip", value = "服务器ip", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "userAccount", value = "服务器账户", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "passwd", value = "服务器密码", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "shareFolderName", value = "文件目录", required = true, paramType = "path", dataTypeClass = String.class), @ApiImplicitParam(name = "remoteFileName", value = "文件名", required = true, paramType = "path", dataTypeClass = String.class)})
    @GetMapping("/deleteFile")
    public FileTransferResponse<String> deleteFile(String ip, String userAccount, String passwd, String shareFolderName, String remoteFileName) {
        logger.info("deleteFile,ip:{},account:{},passwd:{},sharefoldername:{}", ip, userAccount, passwd, shareFolderName);
        fileTransferService.deleteFile(ip, userAccount, passwd, shareFolderName, remoteFileName);
        return FileTransferResponse.success("res");
    }
}